//
//  TwoViewController.m
//  cc
//
//  Created by ZLQ on 2016/10/24.
//  Copyright © 2016年 hbyj. All rights reserved.
//

#import "TwoViewController.h"
#import "SDAssetsTableViewControllerCell.h"
#import "SDAssetsTableViewControllerCellModel.h"
#import "SDAssetsTableViewHeader.h"
#import "SDYuEBaoTableViewController.h"
#import "CCGeRenZhongXinController.h"
#import "Util.h"
#import "CCLogOutViewController.h"
#import "SVProgressHUD.h"

#import "APay.h"
#import "PaaCreater.h"
@interface TwoViewController ()<JumpToAllInPay,APayDelegate>
{
    SDAssetsTableViewHeader *header;
}
@end

@implementation TwoViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    [self.tableView reloadData];
}
#pragma mark ***释放观察者***
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"getInfo" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"JumpToGR" object:nil];
}

-(void)jump{
    NSLog(@"!!! 调起支付插件 !!!");
    
    //订单数据
    
    NSString *payData = [PaaCreater randomPaa];
    
    //@param mode
    //00 生产环境
    //01 测试环境
    
    //在测试与生产环境之间切换的时候请注意修改mode参数
    
    [APay startPay:payData viewController:self delegate:self mode:@"01"];
    
    //控件支付
    //[APay widgetPay:payData viewController:self delegate:self mode:@"01"];
    
    //绑卡
    [APay bindCard:payData viewController:self delegate:self mode:@"01"];
    
    
    //用户银行卡快捷实名认证
    //[APay verifyUser:payData viewController:self delegate:self mode:@"1"];
    
}

//用户银行卡快捷实名认证回调
- (void)verifyUserResult:(NSString *)result{
    NSLog(@"快捷实名认证回调");
}

//绑卡回调
- (void)bindCardResult:(NSString *)result{
    NSLog(@"绑卡回调");
}

//该接口为代理方法，在支付请求受理取消、失败、成功后退出通联支付揑件将调用该接 口。迒回参数 result 的数据格式为 JSON 字符串。
/*
 payResult Number 支付请求受理结果
 0 –支付受理成功
 1 –支付受理失败
 -1 –取消支付
 注意:同步接口迒回的仅为该笔订单的受理情况。实际 是否扣款成功以异步通知为准。
 
 payAmount Number 整型数字，金额不币种有关 如果是人民币，则单位是分，即 10 元提交时金额应为 1000
 payTime String 订单支付日期时间 精确到秒
 payOrderId String 通联商户支付订单号，总是迒回最后一次尝试支付生成的支付订单号;若未生成支付订单则此处迒回空字符串
 */
- (void)APayResult:(NSString *)result {
    
    NSLog(@"result = %@", result);
    NSArray *parts = [result componentsSeparatedByString:@"="];
    NSError *error;
    NSData *data = [[parts lastObject] dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    NSInteger payResult = [dic[@"payResult"] integerValue];
    NSString *format_string = @"支付结果::支付%@";
    if (payResult == APayResultSuccess) {
        NSLog(format_string,@"成功");
    } else if (payResult == APayResultFail) {
        NSLog(format_string,@"失败 %@",error);
    } else if (payResult == APayResultCancel) {
        NSLog(format_string,@"取消");
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBColor(246, 246, 246);
    self.navigationItem.title  = @"Two";
    
    self.sectionsNumber = 3;
    self.cellClass = [SDAssetsTableViewControllerCell class];
    
    [self setupModel];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getInfo:) name:@"getInfo" object:nil];
    
    header = [[SDAssetsTableViewHeader alloc] init];
    header.delegate = self;
    header.iconView.image = [UIImage imageNamed:@"nvpengyou"];
    self.tableView.tableHeaderView = header;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(jumpToGR) name:@"JumpToGR" object:nil];
    
    NSLog(@"沙河目录是:%@",AccountFiledPath);
}

-(void)getInfo:(NSNotification *)info{
    NSDictionary *dict = [info userInfo];
    header.nameLabel.text = dict[@"name"];
    NSString *phone = dict[@"phone"];
    
    UserEntity *userID = [Util getCurrentUserInfo];
    if (!userID.isLogin) {
       header.accountLabel.text = phone;
    }else{
        NSRange range = NSMakeRange(3, 4);
        NSString *newPhone = [phone stringByReplacingCharactersInRange:range withString:@"****"];
        header.accountLabel.text = newPhone;
        //header.iconView.image = dict[@"icon"];
        
        [dict writeToFile:AccountFiledPath atomically:YES];
    }
}

-(void)jumpToGR{
    //存储登录用户到单例
    UserEntity *userID = [Util getCurrentUserInfo];
    if (!userID.isLogin) {
        CCGeRenZhongXinController *gerenZXVC = [[CCGeRenZhongXinController alloc] init];
        //[self.navigationController pushViewController:gerenZXVC animated:YES];
        [self presentViewController:gerenZXVC animated:YES completion:nil];
    }else{
        [self.navigationController pushViewController:[[CCLogOutViewController alloc] init] animated:YES];
    }
}

- (void)setupModel
{
    // section 0 的model
    SDAssetsTableViewControllerCellModel *model01 = [SDAssetsTableViewControllerCellModel modelWithTitle:@"余额详情" iconImageName:@"09999978Icon" destinationControllerClass:[SDYuEBaoTableViewController class]];
    
    SDAssetsTableViewControllerCellModel *model02 = [SDAssetsTableViewControllerCellModel modelWithTitle:@"信用卡还款" iconImageName:@"20000032Icon" destinationControllerClass:[SDBasicTableViewController class]];
    
    SDAssetsTableViewControllerCellModel *model03 = [SDAssetsTableViewControllerCellModel modelWithTitle:@"我的二维码" iconImageName:@"20000059Icon" destinationControllerClass:[SDBasicTableViewController class]];
    
    // section 1 的model
    SDAssetsTableViewControllerCellModel *model11 = [SDAssetsTableViewControllerCellModel modelWithTitle:@"快速转账" iconImageName:@"20000077Icon" destinationControllerClass:[SDBasicTableViewController class]];
    
    SDAssetsTableViewControllerCellModel *model12 = [SDAssetsTableViewControllerCellModel modelWithTitle:@"个人中心" iconImageName:@"20000110Icon" destinationControllerClass:[SDBasicTableViewController class]];
    
    SDAssetsTableViewControllerCellModel *model13 = [SDAssetsTableViewControllerCellModel modelWithTitle:@"我的保障" iconImageName:@"20000118Icon" destinationControllerClass:[SDBasicTableViewController class]];
    
    // section 2 的model
    SDAssetsTableViewControllerCellModel *model21 = [SDAssetsTableViewControllerCellModel modelWithTitle:@"更多" iconImageName:@"20000180Icon" destinationControllerClass:[SDBasicTableViewController class]];
    
    //model01, 
    self.dataArray = @[@[model01, model02, model03],
                       @[model11, model12, model13],
                       @[model21]];
}

#pragma mark - delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //存储登录用户到单例
    UserEntity *userID = [Util getCurrentUserInfo];
    if (!userID.isLogin) {
        [SVProgressHUD showErrorWithStatus:@"您还未登录"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        return;
    }
    SDAssetsTableViewControllerCellModel *model = [self.dataArray[indexPath.section] objectAtIndex:indexPath.row];
    UIViewController *vc = [[model.destinationControllerClass alloc] init];

    vc.title = model.title;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return (section == self.dataArray.count - 1) ? 10 : 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return HEIGHT / 12;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
